package com.wc.alorithm_blue_bridge._模拟.明码;

import java.math.BigDecimal;
import java.util.Scanner;

/**
 * @Author congege
 * @Description
 * @Date Created in 2023/10/16-14:30\
 * <p>
 * 明码
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String tenWords = "4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 \n" +
                "16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16 \n" +
                "4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 \n" +
                "0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4 \n" +
                "4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64 \n" +
                "16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128 \n" +
                "0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0 \n" +
                "2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0 \n" +
                "1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0 \n" +
                "0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0 ";
        String[] words = tenWords.split("\\n");
        Main main = new Main();
        for (String word : words) {
            String[] wordC = word.split(" ");
            for (int i = 0; i < wordC.length; i+=2) {
                System.out.print(main.twoRadix((Integer.parseInt(wordC[i]))));
                System.out.print(main.twoRadix((Integer.parseInt(wordC[i+1]))));
                System.out.println();
            }
            System.out.println();
        }
        System.out.println("九的九次方等于多少？");
        System.out.println(BigDecimal.valueOf(Math.pow(9,9)));
    }

    public String twoRadix(int num) {
        StringBuffer ans = new StringBuffer();
        boolean negative = false;
        if (num < 0) {
            negative = true;
            num = -num;
        }
        int cnt = 7;
        while (num != 0) {
            ans.insert(0, num % 2);
            num /= 2;
            cnt -= 1;
        }
        while (cnt > 0) {
            ans.insert(0, 0);
            cnt--;
        }
        if (negative) {
            int size = ans.length();
            /**
             * 取反
             */
            for (int i = 0; i < size; i++) {
                if (ans.charAt(i) == '0') {
                    ans.replace(i, i+1, "1");
                } else {
                    ans.replace(i, i+1, "0");
                }
            }
            /**
             * +1
             */
            while (size >= 1 && ans.charAt(size - 1) == '1') {
                ans.replace(size - 1, size, "0");
                size--;
            }
            if (size>=1) {
                ans.replace(size - 1, size, "1");
            }
            if (ans.length() == 8) {
                ans.deleteCharAt(0);
            }
        }
        if (negative) {
            ans.insert(0, 1);
        } else {
            ans.insert(0, 0);
        }

        /**
         * 为了方便显示1
         */
        for (int i = 1; i <= 16; i+=2) {
            ans.insert(i,' ');
        }
        /**
         * 为了方便显示
         */
        ans = new StringBuffer(ans.toString().replace("0"," "));
        return ans.toString();
    }
}
